package indi.wrenn.studentsystem.frame;

import indi.wrenn.studentsystem.bean.User;
import indi.wrenn.studentsystem.dao.ManageHelper;
import indi.wrenn.studentsystem.util.WindowUtil;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * 学生成绩管理系统主界面
 * 提供系统所有功能的菜单导航
 */
public class StudentSystemMainFrame extends JFrame {

	// 菜单栏组件定义
	private JMenuBar menuBar;              // 应用程序菜单栏
	private JMenu student_Management;      // "学生管理"菜单
	private JMenu score_Management;        // "成绩管理"菜单
	private JMenu personal_Management;     // "个人管理"菜单

	// 学生管理菜单项
	private JMenuItem add_Student;         // "添加学生"菜单项
	private JMenuItem query_Student;       // "查询学生信息"菜单项
	private JMenuItem modify_Student;      // "修改学生信息"菜单项
	private JMenuItem delete_Student;      // "删除学生"菜单项

	// 成绩管理菜单项
	private JMenuItem add_Score;           // "添加学生成绩"菜单项
	private JMenuItem modify_Score;        // "修改学生成绩"菜单项
	private JMenuItem query_Score;         // "成绩查询"菜单项
	private JMenuItem score_Statistics;    // "成绩统计"菜单项

	// 个人管理菜单项
	private JMenuItem change_Password;     // "修改密码"菜单项
	private JMenuItem logout;              // "退出登录"菜单项

	private JFrame jf;     // 当前窗口引用
	private User user;     // 当前用户对象

	/**
	 * 构造函数 - 初始化主界面
	 * @param user 当前登录用户
	 */
	public StudentSystemMainFrame(User user){
		super("学生成绩管理系统,欢迎您 "+user.getUsername());
		this.user = user;
		this.jf = this;

		// 初始化菜单栏
		menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);

		// 初始化学生管理菜单
		initStudentManagementMenu();

		// 初始化成绩管理菜单
		initScoreManagementMenu();

		// 初始化个人管理菜单
		initPersonalManagementMenu();

		// 设置窗口属性
		this.setSize(578, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		WindowUtil.setFrameCenter(this);  // 设置窗口居中显示

		// 设置背景图片面板
		ImagePanel imagePanel = new ImagePanel();
		setContentPane(imagePanel);

		// 设置窗口图标
		try {
			Image img = ImageIO.read(this.getClass().getResource("/2.png"));
			this.setIconImage(img);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		this.setVisible(true);  // 设置窗口可见

		// 添加窗口监听器，处理窗口关闭事件
		this.addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(WindowEvent arg0) {}

			@Override
			public void windowIconified(WindowEvent arg0) {}

			@Override
			public void windowDeiconified(WindowEvent arg0) {}

			@Override
			public void windowDeactivated(WindowEvent arg0) {}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// 窗口关闭时更新用户登录状态
				ManageHelper helper = new ManageHelper();
				user.setIsLogin(0);  // 设置登录状态为未登录
				helper.Update_IsLogin(user);
			}

			@Override
			public void windowClosed(WindowEvent arg0) {}

			@Override
			public void windowActivated(WindowEvent arg0) {}
		});
	}

	/**
	 * 初始化学生管理菜单
	 */
	private void initStudentManagementMenu() {
		student_Management = new JMenu("学生管理");
		menuBar.add(student_Management);

		// 添加学生菜单项
		add_Student = new JMenuItem("添加学生");
		add_Student.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AddStudentFrame addStudentFrame = new AddStudentFrame(jf, "添加学生", true);
			}
		});
		student_Management.add(add_Student);

		// 查询学生信息菜单项
		query_Student = new JMenuItem("查询学生信息");
		query_Student.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				QueryStudentFrame queryStudentFrame = new QueryStudentFrame(jf, "查询学生信息", true);
			}
		});
		student_Management.add(query_Student);

		// 修改学生信息菜单项
		modify_Student = new JMenuItem("修改学生信息");
		modify_Student.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModifyStudentFrame modifyStudentFrame = new ModifyStudentFrame(jf, "修改学生信息", true);
			}
		});
		student_Management.add(modify_Student);

		// 删除学生菜单项
		delete_Student = new JMenuItem("删除学生");
		delete_Student.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DeleteStudentFrame deleteStudentFrame = new DeleteStudentFrame(jf, "删除学生", true);
			}
		});
		student_Management.add(delete_Student);
	}

	/**
	 * 初始化成绩管理菜单
	 */
	private void initScoreManagementMenu() {
		score_Management = new JMenu("成绩管理");
		menuBar.add(score_Management);

		// 添加学生成绩菜单项
		add_Score = new JMenuItem("添加学生成绩");
		add_Score.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddStudentScoreFrame frame = new AddStudentScoreFrame(jf, "添加学生成绩", true);
			}
		});
		score_Management.add(add_Score);

		// 修改学生成绩菜单项
		modify_Score = new JMenuItem("修改学生成绩");
		modify_Score.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				UpdateStudentScoreFrame frame = new UpdateStudentScoreFrame(jf, "修改学生成绩", true);
			}
		});
		score_Management.add(modify_Score);

		// 成绩查询菜单项
		query_Score = new JMenuItem("成绩查询");
		query_Score.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				QueryStudentScoreFrame frame = new QueryStudentScoreFrame(jf, "查询学生成绩", true);
			}
		});
		score_Management.add(query_Score);

		// 成绩统计菜单项
		score_Statistics = new JMenuItem("成绩统计");
		score_Statistics.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ScoreAnalyzeOption analyzeOption = new ScoreAnalyzeOption(jf, "成绩分析选项", true);
			}
		});
		score_Management.add(score_Statistics);
	}

	/**
	 * 初始化个人管理菜单
	 */
	private void initPersonalManagementMenu() {
		personal_Management = new JMenu("个人管理");
		menuBar.add(personal_Management);

		// 修改密码菜单项
		change_Password = new JMenuItem("修改密码");
		change_Password.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				UpdatePasswordFrame frame = new UpdatePasswordFrame(jf, "修改密码", true, user);
			}
		});
		personal_Management.add(change_Password);

		// 退出登录菜单项
		logout = new JMenuItem("退出登录");
		logout.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jf.dispose();  // 关闭当前窗口
				// 更新用户登录状态
				ManageHelper helper = new ManageHelper();
				user.setIsLogin(0);  // 设置登录状态为未登录
				helper.Update_IsLogin(user);
				// 重新打开登录窗口
				StudentSystemLoginFrame frame = new StudentSystemLoginFrame();
			}
		});
		personal_Management.add(logout);
	}
}

/**
 * 自定义背景图片面板
 */
class ImagePanel extends JPanel {
	private Image image;

	public ImagePanel() {
		try {
			// 加载背景图片
			image = ImageIO.read(this.getClass().getResource("/1.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (image != null) {
			// 绘制背景图片，适应面板大小
			g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
		}
	}
}